Remember?
Register
Back

Templating

By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page 5.

<< Prev123456789Next >>

Now that we have a template file loaded, we can dynamically replace references. Here is the code:

PHP Code

<?php
    
// Replaces references using __set special method (setting a variable calls this if the variable is not accessible to them)
    // e.x. $template->java="fun"; -- $name=java, $value=fun
    
public function __set($name$value) {
        
$this->current_item['****code****']=str_replace("{".$name."}" $value$this->current_item['****code****']);
    }
?>




The function name for this one is of special interest. It overloads the normal setting of a variable in the class, and makes it do something different if no variable with that name (and public) is present. For example, suppose you set $template->java="fun", that would be the same as calling $template->__set("java", "fun");

In this case, we use that to make an easy interface for setting a variable in the template file by str_replace() the reference (with tags around it), and replacing it with the value given.

Now we are going to get sections (this is where it gets a little more complex, due to the completly expansible nature of this):

PHP Code

<?php
// Function to set section and extract code data from current item
    
public function set_section($section) {
        if (empty(
$this->current_item)) {
            
$this->error("set_section()""No handle set");
        }
        if (empty(
$section)) {
            
$this->error("set_section()""No section given");
        }
        
$found=false;
        
// Search down the level_tracker array until we find the requested section
        
for ($i=$this->current_level;$i>=0;$i--) {
            
$contents=array(); // For reg expression below
            
preg_match('|<{'.$section.'}>(.*)<{/'.$section.'}>|iUs'$this->level_tracker[$i]['****code****'], $contents);
            if (!empty(
$contents[1])) { // If we found the section
                
$found=true;
                if (
$i == 0) {
                    
$this->current_item=&$this->level_tracker[$i][$section]; // set current item reference
                
} else {
                    
// If no current iteration for section we are taking this out of, make one
                    
if (empty($this->level_tracker[$i][0]['****code****'])) {
                        
$this->level_tracker[$i][0]['****code****']=$this->level_tracker[$i]['****code****'];
                    }
                    
$num=$this->level_tracker[$i]['****current_iteration****']; // for below
                    
$this->current_item=&$this->level_tracker[$i][$num][$section]; // set current item
                
}
                
$this->level_tracker[$i+1]=&$this->current_item// add this to level_tracker
                
$this->current_level=$i+1// update current level
                
if (empty($this->current_item['****code****'])) { // not exist yet, setup
                    
$this->current_item['****code****']=$contents[1];
                    
$this->current_item['****current_iteration****']=0;
                }
                break;
            }
        }
        if (!
$found) {
            
$this->error("set_section()""No section found for '$section'");
        }
    }
?>




Here we are using a section name, and fetching data for it down the $level_tracker array. We check if each level has the section in the code, and stop at the highest level that has the section, grab the stuff inside of it, and set that in the $code array where it belongs.

<< Prev123456789Next >>